package com.tjbank.cssys.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkCreditCivilCirminalCases;
import com.tjbank.cssys.repository.CkCreditCivilCirminalCasesMapper;
import com.tjbank.cssys.repository.CkCreditOutCompanyDataLogMapper;
import com.tjbank.cssys.repository.CkOutsourcingCompanyMapper;
import com.tjbank.cssys.web.config.Constant;
import com.tjbank.cssys.web.service.CkCreditCivilCirminalCasesService;
import com.tjbank.cssys.web.util.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CkCreditCivilCirminalCasesServiceImpl implements CkCreditCivilCirminalCasesService {
    private static Logger logger = LoggerFactory.getLogger(CkCreditCivilCirminalCasesServiceImpl.class);
    @Autowired
    private CkOutsourcingCompanyMapper ckOutsourcingCompanyMapper;
    @Autowired
    private CkCreditCivilCirminalCasesMapper ckCreditCivilCirminalCasesMapper;
    @Autowired
    private CkCreditOutCompanyDataLogMapper ckCreditOutCompanyDataLogMapper;

    @Override
    public CommonResult uploadCivilCirminal(JSONObject jsonObject, String busstype, String fileName) {
        logger.info("上传民事刑事案件--start---jsonObject:" + jsonObject + " ---bussy:" + busstype + "----fileName:" + fileName);
        if (jsonObject.size() == 0) {
            return CommonResult.success(null, "上传的数据是空");
        }
        JSONArray jsonArray = jsonObject.getJSONArray("jsonArray");
        if (null == jsonArray || jsonArray.size() == 0) {
            return CommonResult.success(null, "上传的数据是空");
        }
        List<CkCreditCivilCirminalCases> civilCirminalCasesList = jsonArray.toJavaList(CkCreditCivilCirminalCases.class);
        if (civilCirminalCasesList.size() == 0) {
            return CommonResult.success(null, "上传的数据是空");
        }
        String resultCheck = checkCivilCirminal(civilCirminalCasesList);
        if (resultCheck != null) {
            return CommonResult.success(null, resultCheck);
        }
        civilCirminalCasesList = saveDomainValue(civilCirminalCasesList, busstype, fileName);
        for (CkCreditCivilCirminalCases civilCirminalCases : civilCirminalCasesList) {
            int resultCount = ckCreditCivilCirminalCasesMapper.IsNotHave(civilCirminalCases);
            if (resultCount == 0) {
                ckCreditCivilCirminalCasesMapper.insertSelective(civilCirminalCases);
            } else {
                ckCreditCivilCirminalCasesMapper.updateHave(civilCirminalCases);
            }
        }
        return CommonResult.success(null, "上传成功");
    }


    /***
     * 验证上传数据
     * @param list
     * @return
     */
    private String checkCivilCirminal(List<CkCreditCivilCirminalCases> list) {
        StringBuffer stringBuffer = new StringBuffer("");
        Map<String, String> map = new HashMap<>();
        int countList = 0;
        for (CkCreditCivilCirminalCases ckCreditCivilCirminalCases : list) {
            int row = ++countList;
            if (ckCreditCivilCirminalCases.getCardNumber() == null || "".equals(ckCreditCivilCirminalCases.getCardNumber())) {
                stringBuffer.append(row + "-");
                stringBuffer.append("卡号是空,");
            } else {
                Map<String, String> allowType = ckOutsourcingCompanyMapper.getCardNumber(ckCreditCivilCirminalCases.getCardNumber());
                if (null == allowType || allowType.size() == 0) {
                    stringBuffer.append(row + "-");
                    stringBuffer.append("卡号有误,");
                } else {
                    ckCreditCivilCirminalCases.setCaseType(allowType.get("allow_type"));
                }
            }
            if (null == ckCreditCivilCirminalCases.getOldBatchNumber() || "".equals(ckCreditCivilCirminalCases.getOldBatchNumber()) || ckCreditCivilCirminalCases.getOldBatchNumber().length() != 8) {
                stringBuffer.append(row + "-");
                stringBuffer.append("原批次号是空或填写有误,");
            }
            if ("".equals(ckCreditCivilCirminalCases.getOutCompanyName()) || null == ckCreditCivilCirminalCases.getOutCompanyName()) {
                stringBuffer.append(row + "-");
                stringBuffer.append("公司名是空，");
            } else {
                Map<String, String> castType = ckOutsourcingCompanyMapper.getOutCompanyByName(ckCreditCivilCirminalCases.getOutCompanyName());
                if (null == castType || castType.size() == 0) {
                    stringBuffer.append(row + "-");
                    stringBuffer.append("公司名称不正确");
                }
            }

            /*map.put("cardNumber", ckCreditCivilCirminalCases.getCardNumber());
            map.put("batchNumber", ckCreditCivilCirminalCases.getOldBatchNumber());
            map.put("outCompanyName", ckCreditCivilCirminalCases.getOutCompanyName());
            map.put("zhiInstitution", ckCreditCivilCirminalCases.getZhiInstitution());
            map.put("fenInstitution", ckCreditCivilCirminalCases.getFenInstitution());
            int countResult = ckCreditOutCompanyDataLogMapper.getDataLogByParam(map);
            map.clear();
            if (countResult == 0) {
                stringBuffer.append(row + "-");
                stringBuffer.append("卡号，批次号，公司名，支行机构，分行机构，填写有误");
            }*/
        }
        if (stringBuffer.length() > 1) {
            return stringBuffer.toString();
        }
        return null;
    }

    private List<CkCreditCivilCirminalCases> saveDomainValue(List<CkCreditCivilCirminalCases> list, String bussType, String fileName) {
        for (CkCreditCivilCirminalCases ckCreditCivilCirminalCases : list) {
            ckCreditCivilCirminalCases.setBussType(bussType);
            ckCreditCivilCirminalCases.setCurrDate(new Date());
            ckCreditCivilCirminalCases.setFileName(fileName);
            ckCreditCivilCirminalCases.setCaseStatue("1");
            if (!ckCreditCivilCirminalCases.getOldBatchNumber().substring(6, 8).equals("15")) {
                ckCreditCivilCirminalCases.setBatchNumber(Constant.civilOneBatch);
            } else {
                ckCreditCivilCirminalCases.setBatchNumber(Constant.civilWuBatch);
            }
            if (null != ckCreditCivilCirminalCases.getExecuteNumber() && !"".equals(ckCreditCivilCirminalCases.getExecuteNumber())) {
                ckCreditCivilCirminalCases.setProgressCases("执行终结");
                ckCreditCivilCirminalCases.setCaseStatue("2");
            } else if (null != ckCreditCivilCirminalCases.getJudgementNumber() && !"".equals(ckCreditCivilCirminalCases.getJudgementNumber())) {
                ckCreditCivilCirminalCases.setProgressCases("已判决");
            } else if (null != ckCreditCivilCirminalCases.getLegalCost() && !"".equals(ckCreditCivilCirminalCases.getLegalCost())) {
                ckCreditCivilCirminalCases.setProgressCases("已立案");
            } else {
                ckCreditCivilCirminalCases.setProgressCases("已发起诉讼");
            }
        }
        return list;
    }


    @Override
    public PageInfo listData(Map<String, String> params) {
        logger.info("诉讼案件列表数据查询---start,params=" + JSON.toJSONString(params));
        int page = Integer.parseInt(params.getOrDefault("page", "1").toString());
        int pageSize = Integer.parseInt(params.getOrDefault("pageSize", "10").toString());

        PageHelper.startPage(page, pageSize);
        List<CkCreditCivilCirminalCases> datas = ckCreditCivilCirminalCasesMapper.listData(params);

        PageInfo result = new PageInfo(datas);
        logger.info("诉讼案件列表数据查询---end,PageInfo=" + JSON.toJSONString(result));
        return result;
    }


}
